package com.examinantsdk.base

import android.R
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity

object ToastUtil {
    private val handler = Handler(Looper.getMainLooper())
    private var toast: Toast? = null

    fun showToast(activity: ComponentActivity, message: String, isLong: Boolean = false, gravity: Int = Gravity.CENTER) {
        toastMessage(activity, message, isLong, gravity)
    }

    private fun toastMessage(activity: ComponentActivity, message: String, isLong: Boolean, gravity: Int) {
        handler.post {
            if (toast != null) {
                toast?.cancel()
                toast = null
            }
            toast = Toast.makeText(activity, message, if (isLong) Toast.LENGTH_LONG else Toast.LENGTH_SHORT)
            toast?.setGravity(gravity, 0, 0)
//            val view = toast?.view
//            if (view != null) {
//                val textView = view.findViewById<TextView>(R.id.message)
//                if (textView != null) {
//                    textView.gravity = Gravity.CENTER
//                }
//            }
            toast?.show()
        }
    }
}